﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Bamboo.Utils;

namespace Bamboo.ExeShell
{
    public static class CurlEx
    {
        /// <summary>
        /// 使用curl.exe上传文件或目录
        /// </summary>
        /// <param name="p"></param>
        /// <param name="curlPath">curl.exe的路径，填空则根据系统Path环境变量自动查找</param>
        /// <param name="localPath">本地文件或目录</param>
        /// <param name="url">协议和远端路径，例如：sftp://192.168.88.8:22/subdir/</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="autoCreateRemoteDir">是否自动创建服务器上的目录</param>
        public static void CurlUpload(this ProcessExecute p, string curlPath, string localPath, string url, 
            string username, string password, bool autoCreateRemoteDir)
        {
            localPath = Path.GetFullPath(localPath);
            if (!File.Exists(localPath)) throw new Exception("找不到要上传的本地文件：" + localPath);
            p.FileName = string.IsNullOrWhiteSpace(curlPath) ? "curl.exe" : curlPath;
            string p1 = autoCreateRemoteDir ? "--ftp-create-dirs" : "";
            p.Arguments = p1 + " -k -u \"" + username + ":" + password.Replace("\"","\\\"") + "\" -T \"" +
                localPath + "\" " + url;
            p.Run();

        }
    }

}
